﻿using grproLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DealPrint
{
    /// <summary>
    /// 报表模板使用存储过程传参方式获取数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ReportParamSQL<T> where T : class
    {
        //定义Grid++Report报表主对象
        private GridppReport Report = new GridppReport();
        /// <summary>
        /// 传参数据
        /// </summary>
        private Dictionary<string, string> dicParamData = null;

        /// <summary>
        /// 初始化报表对象
        /// </summary>
        /// <param name="param">存储过程查询参数对象</param>
        /// <param name="reportFile">打印模板路径(查询SQL中调用存储过程)</param>
        /// <param name="printerName">打印机名称</param>
        ///// <param name="connectString">数据库连接字符串</param>
        public ReportParamSQL(T param, string reportFile, string printerName)
        {
            Report.LoadFromFile(reportFile);

            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            //Report.DetailGrid.Recordset.ConnectionString = connectString;

            //设置打印机，未设置则按默认打印机
            if (!string.IsNullOrWhiteSpace(printerName))
                Report.Printer.PrinterName = printerName;

            //获取T中的属性值
            PropertyInfo[] propList = typeof(T).GetProperties();
            if (propList.Length > 0)
            {
                dicParamData = new Dictionary<string, string>();
                foreach (PropertyInfo item in propList)
                {
                    string propName = item.Name;
                    object value = item.GetValue(param, null);//用pi.GetValue获得值
                    string propValue = value == null ? string.Empty : value.ToString();
                    ;
                    if (!dicParamData.ContainsKey(propName))
                    {
                        dicParamData.Add(propName, propValue);
                    }
                }
            }
        }

        /// <summary>
        /// 报表打印
        /// <param name="isPreview">是否预览打印</param>
        /// </summary>
        public bool ReportPrint(bool isPreview)
        {
            try
            {
                //添加处理委托(绑定存储过程查询参数值)
                Report.Initialize += new grproLib._IGridppReportEvents_InitializeEventHandler(ReportInitialize);

                //打印
                if (isPreview)
                {
                    Report.PrintPreview(true);
                }
                else
                {
                    Report.Print(false);
                }

                Report.Initialize -= new grproLib._IGridppReportEvents_InitializeEventHandler(ReportInitialize);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void ReportInitialize()
        {
            foreach (var keyValue in dicParamData)
            {
                Report.ParameterByName(keyValue.Key).AsString = keyValue.Value;
            }
        }


    }
}
